package com.dream.keigezhushou.Activity.acty.play.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dream.keigezhushou.Activity.Uiutils.UiUtils;
import com.dream.keigezhushou.Activity.acty.play.VideoInfoActivity;
import com.dream.keigezhushou.R;
import com.squareup.picasso.Picasso;

import butterknife.BindView;
import butterknife.ButterKnife;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

/**
 *  最新视频
 */

public class NewVideoFragment extends Fragment {

    @BindView(R.id.lv_hot_video)
    ListView lvHotVideo;
    private String[] videoUrl = {"http://gslb.miaopai.com/stream/7b6u5oHU-2-Y3pEm1-ezIlpry5w_.mp4",
            "http://gslb.miaopai.com/stream/1J8R902QZs8JiEWki6l458kfsk6o9hmh.mp4",
            "http://gslb.miaopai.com/stream/Jy6HbulSRp-LdDKlrCPk5nHsVAA_.mp4",};
    private String[] postUrl = {
            "http://wsqncdn.miaopai.com/stream/7b6u5oHU-2-Y3pEm1-ezIlpry5w__m.jpg",
            "http://dlqncdn.miaopai.com/stream/1J8R902QZs8JiEWki6l458kfsk6o9hmh_m.jpg",
            "http://dlqncdn.miaopai.com/stream/Jy6HbulSRp-LdDKlrCPk5nHsVAA__m.jpg"};
    private String[] tvDesc = {"心中所爱，紫霞仙子。。","疯狂的不仅仅人类，动物也疯狂#疯狂动物城#","魔性音乐，魔性舞蹈~两个妹子玩疯了。。"};
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = UiUtils.inflateView(R.layout.layout_hot_video);
        ButterKnife.bind(this, view);

        initData();
        return view;
    }


    //初始化数据
    protected void initData(){

        //ListView创建适配器
        lvHotVideo.setAdapter(new HotAdapter(getContext()));

    }

    public class HotAdapter extends BaseAdapter {

        private Context mContext;
        public HotAdapter(Context context) {
            this.mContext = context;
        }

        @Override
        public int getCount() {
            return videoUrl.length;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            ViewHolder mHolder = null;
            if (view == null){

                view = LayoutInflater.from(mContext).inflate(R.layout.layout_play_item,null);

                mHolder = new ViewHolder();
                mHolder.ivUserPic = (ImageView) view.findViewById(R.id.iv_user_pic);
                mHolder.tvUserNick = (TextView)view.findViewById(R.id.tv_user_nick);
                mHolder.tvDate = (TextView)view.findViewById(R.id.tv_date);
                mHolder.tvDesc = (TextView)view.findViewById(R.id.tv_describe);
                mHolder.tvEvaluate = (TextView)view.findViewById(R.id.tv_evaluate);
                mHolder.tvShare = (TextView)view.findViewById(R.id.tv_share);
                mHolder.videoPlayer = (JCVideoPlayerStandard)view.findViewById(R.id.video_player);
                view.setTag(mHolder);

            }else {

                mHolder = (ViewHolder)view.getTag();
            }


            mHolder.videoPlayer.setUp(videoUrl[i], JCVideoPlayer.SCREEN_LAYOUT_LIST);

            Picasso.with(mContext)
                    .load(postUrl[i])
                    .into(mHolder.videoPlayer.thumbImageView);
            mHolder.tvDesc.setText(tvDesc[i]);

            mHolder.tvEvaluate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Intent intent = new Intent(mContext, VideoInfoActivity.class);
                    startActivity(intent);
                }
            });
            return view;
        }

        class ViewHolder{

            ImageView ivUserPic;
            TextView tvUserNick,tvDate,tvDesc,tvEvaluate,tvShare;
            JCVideoPlayerStandard videoPlayer;

        }
    }
}
